Chapter 6 — Data Loading and File Formats
Code Reference File — Copy and paste as needed

========================================
Setup — Mount Google Drive
========================================
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd

========================================
6.1.1 Writing CSV
========================================
customers.to_csv('customers_export.csv', index=False)

========================================
6.1.2 Reading CSV
========================================
customers_loaded = pd.read_csv('customers_export.csv')
print(customers_loaded.head())

========================================
6.1.3 Writing JSON
========================================
customers.to_json('customers_export.json')

========================================
6.1.4 Reading JSON
========================================
customers_json = pd.read_json('customers_export.json')
print(customers_json.head())

========================================
6.2.1 Writing Excel
========================================
customers.to_excel('customers_export.xlsx', index=False)

========================================
6.2.2 Reading Excel
========================================
customers_excel = pd.read_excel('customers_export.xlsx')
print(customers_excel.head())

========================================
6.3 Loading sales_data.xlsx
========================================
file_path = '/content/drive/MyDrive/sales_data.xlsx'
countries = pd.read_excel(file_path, sheet_name='Countries')
product   = pd.read_excel(file_path, sheet_name='Product')
sales     = pd.read_excel(file_path, sheet_name='Sales')
print(countries.head())
print(product.head())
print(sales.head())

========================================
6.4.1 Loading CSV from URL
========================================
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv'
tips = pd.read_csv(url)
tips.head()

========================================
6.4.2 Loading Web Table
========================================
import requests
url = 'https://www.dynamicwebtraining.com.au/power-bi-training-courses'
html = requests.get(url).text
tables = pd.read_html(html)
tables[0]

========================================
6.5.1 JSON API
========================================
import requests
response = requests.get('https://api.sampleapis.com/coffee/hot')
df = pd.DataFrame(response.json())
df.head()

========================================
6.6.1 SQLite
========================================
import sqlite3
conn = sqlite3.connect(':memory:')
sales.to_sql('sales', conn, if_exists='replace', index=False)
df = pd.read_sql_query('SELECT * FROM sales', conn)
df.head()
